package com.damie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dadie.Domain.ShopOrderProductDomain;
import com.dadie.Dto.ShopOrderDto;
import com.dadie.Dto.ShopOrderProductDto;
import com.dadie.parame.ShopOrderParame;
import com.damie.common.commonutils.R;
import com.damie.common.jwtuilts.JwtUtil;
import com.damie.service.ShopOrderProductService;
import com.damie.service.ShopOrderService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Alon
 * @since 2021-01-24
 */
@Api(value="商城controller",tags={"后台:商城订单管理"})
@RestController
@CrossOrigin("*")
@RequestMapping("/damie/shop-order")
public class ShopOrderController {

    @Resource
    private ShopOrderService shopOrderService;

    @Resource
    private ShopOrderProductService shopOrderProductService;

    @ApiOperation("分页条件查询所有商城订单")
    @PostMapping("selectAllOrder")
    public R selectAllOrder(Integer index, Integer size, @RequestBody ShopOrderParame shopOrderParame, HttpServletRequest request){

        if (request.getHeader("token")!=null&&!request.getHeader("token").equals("")){
            String token = request.getHeader("token");
            Claims claims = JwtUtil.parseToken(token);
            Object userId = claims.get("userId");
            shopOrderService.selectAllOrder(index, size, shopOrderParame,userId.toString());
        }
        Page<ShopOrderDto> shopOrderDtos = shopOrderService.selectAllOrder(index, size, shopOrderParame);
        return R.ok().data("shopOrderList",shopOrderDtos);
    }

    @GetMapping("selectByOrderProduct")
    @ApiOperation("查询某个商城订单所购买的商品")
    public R selectByOrderProduct(String id){
        List<ShopOrderProductDto> shopOrderDtos = shopOrderProductService.selectByOrderProduct(id);
        return R.ok().data("shopOrderList",shopOrderDtos);
    }

}

